package com.sky.controller.user;

import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wangqi
 * @date 2024/1/4 09:03
 */
@RestController
@RequestMapping("/user/addressBook")
@Slf4j
public class AddressController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        List<AddressBook> list = addressBookService.list();
        return Result.success(list);
    }

    @PostMapping
    public Result save(@RequestBody AddressBook addressBook){
        log.info("添加地址：{}",addressBook);
        addressBookService.insert(addressBook);
        return Result.success();
    }

    @PutMapping("/default")
     public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认地址：{}",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
     }

     @GetMapping("/{id}")
     public Result<AddressBook> getAddressById(@PathVariable Long id) {
        log.info("根据地址id查询地址详情：id={}",id);
         AddressBook addressBook = addressBookService.findAddressById(id);
        return Result.success(addressBook);
     }

     @PutMapping
     public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址：{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
     }

     @DeleteMapping
     public Result delete(Long id){
        log.info("删除地址: id={}",id);
        addressBookService.delete(id);
        return Result.success();
     }


}
